package com.dxg.msns.comment.mapper;

import com.dxg.msns.comment.pojo.Comment;
import org.apache.commons.lang.ArrayUtils;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

@org.apache.ibatis.annotations.Mapper
public interface CommentMapper extends Mapper<Comment> {

    @Insert("INSERT INTO comment (comment_id,pid, lid, dynamic_id, reply_id, respondent_id, comment_date, comment_content, status, music_id, singer_id, playlist_id) " +
            "VALUES (#{commentId},#{pid}, #{lid}, #{dynamicId}, #{replyId}, #{respondentId}, #{commentDate}, #{commentContent}, #{status}, #{musicId}, #{singerId}, #{playlistId})")
    void insertComment(Comment comment);

    @Select("SELECT * FROM comment WHERE comment_id = #{commentId}")
    @ResultMap("commentMap")
    Comment selectByCommentId(@Param("commentId") String commentId);

    // 公共结果映射定义
    @Select("SELECT * FROM comment WHERE id = #{id}")
    @Results(id = "commentMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "comment_id", property = "commentId"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "lid", property = "lid"),
            @Result(column = "dynamic_id", property = "dynamicId"),
            @Result(column = "reply_id", property = "replyId"),
            @Result(column = "respondent_id", property = "respondentId"),
            @Result(column = "comment_date", property = "commentDate"),
            @Result(column = "comment_content", property = "commentContent"),
            @Result(column = "status", property = "status"),
            @Result(column = "music_id", property = "musicId"),
            @Result(column = "singer_id", property = "singerId"),
            @Result(column = "playlist_id", property = "playlistId")
    })
    Comment getCommentById(Long id); // 只是为了定义 commentMap，可忽略此方法的调用

    @Select("SELECT * FROM comment WHERE music_id = #{musicId} AND pid IS NULL")
    @ResultMap("commentMap")
    List<Comment> selectFatherCommentsByMusicId(@Param("musicId") String musicId);

    @Select("SELECT * FROM comment WHERE singer_id = #{singerId} AND pid IS NULL")
    @ResultMap("commentMap")
    List<Comment> selectFatherCommentsBySingerId(@Param("singerId") String singerId);

    @Select("SELECT * FROM comment WHERE playlist_id = #{playlistId} AND pid IS NULL")
    @ResultMap("commentMap")
    List<Comment> selectFatherCommentsByPlaylistId(@Param("playlistId") String playlistId);

    @Select("SELECT * FROM comment WHERE pid = #{commentId}")
    @ResultMap("commentMap")
    List<Comment> selectSonCommentsByParentCommentId(@Param("commentId") String commentId);

    @Select("select max(comment_date) from comment where status = '1' and dynamic_id = #{dynamic_id} group by dynamic_id")
    Date queryRecentCreateDateByDynamicId(@Param("dynamic_id") Integer dynamicId);

    @Update("update comment set status = '-1' where id = #{id}")
    void updateDeleteStateById(@Param("id") Long id);

    @UpdateProvider(type = Provider.class, method = "batchUpdate")
    void updateStateByIds(Long[] ids, String status, String respondentId);

    class Provider {
        public String batchUpdate(Map map) {
            Long[] ids = (Long[]) map.get("param1");
            String status = (String) map.get("param2");
            String respondentId = (String) map.get("param3");
            StringBuilder sb = new StringBuilder();
            if (ArrayUtils.isEmpty(ids)) {
                sb.append("update comment set status = '").append(status)
                        .append("' where respondent_id = '").append(respondentId)
                        .append("' and status != '-1' and status != '0'");
            } else {
                sb.append("update comment set status = '").append(status)
                        .append("' where respondent_id = '").append(respondentId)
                        .append("' and status != '-1' and status != '0' and id in (");
                for (int i = 0; i < ids.length; i++) {
                    sb.append(ids[i]);
                    if (i < ids.length - 1) {
                        sb.append(",");
                    }
                }
                sb.append(")");
            }
            return sb.toString();
        }
    }
}
